//index.js
//获取应用实例
var app = getApp()
Page({
  data: {
    sortType: 6,
    showLoading: true,
    noMoretip: false,
    page: 0,
    curListType:4,
    pricesort:''
  },
  onLoad: function (e) {
    var that = this;
    app.setNavColor(that);
    if (e && e.id) {
      that.setData({
        id: e.id
      })
    }
    var title = '可用商品'
    if (e && e.title) {
      title = e.title;
    }
    app.setNavtitle(title);
    that.requestAllGoods();
  },
  onShow: function () {
    var that = this;
  },
  requestAllGoods: function (){
    var that = this;
    var data = {};
    var page = that.data.page;
    data.map = 'applet_community_coupon_goodsList';
    data.id = that.data.id;
    data.page = page;
    console.log(data);
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        console.log(res.data);
        if (res.data.ec == 200) {
          var allArr = [];
          var initArr = that.data.shopGoods ? that.data.shopGoods:'[]';
          var curArr = res.data.data;
          var lastPageLength = curArr.length;
          if (page > 0) {
            allArr = initArr.concat(curArr);
          } else {
            allArr = res.data.data;
          }
          that.setData({
            shopGoods: allArr
          })
          if (lastPageLength < 10) {
            that.setData({
              noMoretip: true,
              showLoading: false
            });
          }
          console.log(that.data.shopGoods);
        } else {
          console.log(res.data)
          if (page <= 0) {
            that.setData({
              shopGoods: []
            })
          } else {
            that.setData({
              noMoretip: true,
              showLoading: false
            });
          }
        }
      },
      complete: function () {
        wx.hideToast();
        wx.stopPullDownRefresh();
      }
    });
  },
  onPullDownRefresh: function () {
    var that = this;
    var oneid = that.data.oneid;
    var secondid = that.data.secondid;
    that.setData({
      page: 0,
      noMoretip: false,
      showLoading: true
    });
    that.requestAllGoods(oneid, secondid);
    console.log("下拉刷新");
  },
  onReachBottom: function () {
    var that = this;
    var oneid = that.data.oneid;
    var secondid = that.data.secondid;
    console.log("到达页面底部")
    var isMore = that.data.noMoretip;
    var page = that.data.page;
    page++;
    that.setData({
      page: page
    });
    if (isMore) {
      console.log("已完成或正在加载");
    } else {
      that.requestAllGoods(oneid, secondid);
    }
  },
  goodDetail: function (e) {
    var goodId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/subpages1/goodDetail/goodDetail?id=' + goodId
    })
  },
  changeShowType: function (e) {
    var type = e.target.dataset.type;
    this.setData({
      showType: type
    })
  },
  searchPage: function () {
    wx.navigateTo({
      url: '/subpages1/searchList/searchList'
    })
  },
  sortGood: function (e) {
    var that = this;
    var type = e.target.dataset.sort;
    var pricesort = that.data.pricesort;
    if(type==1){
      if (pricesort == 'up'){
        pricesort = 'down';
      } else if (pricesort = 'down'){
        pricesort = 'up';
      }else{
        pricesort = 'up';
      }
    }else{
      pricesort = '';
    }
    this.setData({
      sortType: type,
      pricesort: pricesort
    })
    this.onPullDownRefresh();
  },
  toggleListshow:function(){
    var that = this;
    var curListType = that.data.curListType;
    if (curListType==2){
      curListType = 4;
    } else if(curListType==4){
      curListType = 2;
    }
    that.setData({
      curListType: curListType
    })
  }
})